Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / util / StaleWhileRevalidateFlow.kt

Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/util/StaleWhileRevalidateFlow.kt 01cd54907d62cd90913d3d071b6bc240cae365ef (01cd5490) Text, 3.58 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.util

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.flow
Tff7b72import T7ee787kotlinx.coroutines.flow.flowOn
Tff7b72import T7ee787kotlinx.coroutines.withTimeoutOrNull
Tff7b72import T7ee787org.meshtastic.core.common.util.safeCatching
Tff7b72import T7ee787kotlin.coroutines.CoroutineContext

Tff7b72private Tff7b72const Tff7b72val Te6edf3DEFAULT_NETWORK_TIMEOUT_MS Tff7b72= T79c0ff5Te6edf3_000L

T8b949e/**
* Creates a cold Flow that implements the stale-while-revalidate caching pattern:
* 1. Load and emit cached data immediately (UI never waits for network).
* 2. If [shouldFetch] returns true, attempt a refresh optionally bounded by [networkTimeoutMs].
* 3. Reload from cache and emit again if the data changed.
*
* The [fetch] lambda is expected to write results to the local cache as a side-effect; [loadFromCache] is called again
* after fetch to pick up the fresh data.
*
* All work runs on [context] (typically an IO dispatcher).
*
* @param T The domain model type emitted to collectors.
* @param loadFromCache Loads the current best-available data from local storage.
* @param shouldFetch Decides whether a network refresh is needed based on the cached value (suspendable).
* @param fetch Performs the network call and persists results locally (side-effect only).
* @param context The coroutine context for cache/network operations.
* @param networkTimeoutMs Maximum time to wait for [fetch] before falling back to cached data. Pass null when [fetch]
* bounds its own network work and also performs local cache writes that must not be cancelled by this helper.
* @param tag Logging tag for diagnostics.
*/
Tff7b72internal Tff7b72fun Tff7b72<Te6edf3T Tb4b4b4: Te6edf3AnyTff7b72> Td2a8ffstaleWhileRevalidateFlowTb4b4b4(
Te6edf3loadFromCacheTb4b4b4: Te6edf3suspend Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Te6edf3T?Tb4b4b4,
Te6edf3shouldFetchTb4b4b4: Te6edf3suspend Tb4b4b4(Te6edf3T?Tb4b4b4) Tff7b72-Tff7b72> Tffa657BooleanTb4b4b4,
Te6edf3fetchTb4b4b4: Te6edf3suspend Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4,
Te6edf3contextTb4b4b4: Te6edf3CoroutineContextTb4b4b4,
Te6edf3networkTimeoutMsTb4b4b4: Tffa657Long? Tff7b72= Te6edf3DEFAULT_NETWORK_TIMEOUT_MSTb4b4b4,
Te6edf3tagTb4b4b4: Tffa657String Tff7b72= Ta5d6ff"Ta5d6ffStaleWhileRevalidateTa5d6ff"Tb4b4b4,
Tb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Te6edf3T?Tff7b72> Tff7b72= Te6edf3flow Tb4b4b4{
Tff7b72val Te6edf3cached Tff7b72=
Te6edf3safeCatching Tb4b4b4{ Te6edf3loadFromCacheTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3tagTa5d6ff: cache read failedTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4.Te6edf3getOrNullTb4b4b4(Tb4b4b4)
Te6edf3emitTb4b4b4(Te6edf3cachedTb4b4b4)

Tff7b72if Tb4b4b4(Tff7b72!Te6edf3shouldFetchTb4b4b4(Te6edf3cachedTb4b4b4)Tb4b4b4) Tff7b72returnTf0883e@flow

Tff7b72suspend Tff7b72fun Td2a8ffrunFetchTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{ Te6edf3fetchTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3tagTa5d6ff: network fetch failedTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4}

Tff7b72val Te6edf3timedOut Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3networkTimeoutMs Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3runFetchTb4b4b4(Tb4b4b4)
Tff7b72false
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3withTimeoutOrNullTb4b4b4(Te6edf3networkTimeoutMsTb4b4b4) Tb4b4b4{
Te6edf3runFetchTb4b4b4(Tb4b4b4)
Tff7b72true
Tb4b4b4} Tff7b72!Tff7b72= Tff7b72true
Tb4b4b4}

Tff7b72if Tb4b4b4(Te6edf3timedOutTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3tagTa5d6ff: network fetch timed out after Tffd700${Te6edf3networkTimeoutMsTffd700}Ta5d6ffmsTa5d6ff" Tb4b4b4}
Tb4b4b4}

Tff7b72val Te6edf3fresh Tff7b72=
Te6edf3safeCatching Tb4b4b4{ Te6edf3loadFromCacheTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3tagTa5d6ff: cache reload failed after fetchTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4.Te6edf3getOrElse Tb4b4b4{ Te6edf3cached Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3fresh Tff7b72!Tff7b72= Te6edf3cachedTb4b4b4) Tb4b4b4{
Te6edf3emitTb4b4b4(Te6edf3freshTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3flowOnTb4b4b4(Te6edf3contextTb4b4b4)

Served by rngit 1.5.0 - Generated in 0.06s